<template>
  <div class="contact-us">
    <h1>联系我们</h1>
    <p class="subtitle">如果您有任何问题或建议，请通过下方表单与我们联系</p>
    
    <form @submit.prevent="handleSubmit" class="contact-form">
      <div class="form-group">
        <label for="name">姓名</label>
        <input 
          type="text" 
          id="name" 
          v-model="formData.name" 
          @blur="validateField('name')"
          :class="{ 'error': errors.name }"
        />
        <span class="error-message" v-if="errors.name">{{ errors.name }}</span>
      </div>
      
      <div class="form-group">
        <label for="email">电子邮箱</label>
        <input 
          type="email" 
          id="email" 
          v-model="formData.email" 
          @blur="validateField('email')"
          :class="{ 'error': errors.email }"
        />
        <span class="error-message" v-if="errors.email">{{ errors.email }}</span>
      </div>
      
      <div class="form-group">
        <label for="subject">主题</label>
        <input 
          type="text" 
          id="subject" 
          v-model="formData.subject" 
          @blur="validateField('subject')"
          :class="{ 'error': errors.subject }"
        />
        <span class="error-message" v-if="errors.subject">{{ errors.subject }}</span>
      </div>
      
      <div class="form-group">
        <label for="message">留言内容</label>
        <textarea 
          id="message" 
          v-model="formData.message" 
          @blur="validateField('message')"
          :class="{ 'error': errors.message }"
          rows="5"
        ></textarea>
        <span class="error-message" v-if="errors.message">{{ errors.message }}</span>
      </div>
      
      <button type="submit" :disabled="isSubmitting" class="submit-btn">
        {{ isSubmitting ? '提交中...' : '提交留言' }}
      </button>
    </form>
    
    <div v-if="submitSuccess" class="success-message">
      <p>感谢您的留言！我们已经收到您的信息，会尽快与您联系。</p>
    </div>
    
    <div class="contact-info">
      <h3>其他联系方式</h3>
      <p><strong>电话：</strong> 400-123-4567</p>
      <p><strong>邮箱：</strong> contact@example.com</p>
      <p><strong>工作时间：</strong> 周一至周五 9:00-18:00</p>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';

const formData = reactive({
  name: '',
  email: '',
  subject: '',
  message: ''
});

const errors = reactive({
  name: '',
  email: '',
  subject: '',
  message: ''
});

const isSubmitting = ref(false);
const submitSuccess = ref(false);

const validateField = (field) => {
  switch (field) {
    case 'name':
      errors.name = formData.name.trim() === '' ? '请输入您的姓名' : '';
      break;
    case 'email':
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      errors.email = !emailRegex.test(formData.email) ? '请输入有效的电子邮箱' : '';
      break;
    case 'subject':
      errors.subject = formData.subject.trim() === '' ? '请输入留言主题' : '';
      break;
    case 'message':
      errors.message = formData.message.trim() === '' ? '请输入留言内容' : '';
      break;
  }
};

const validateForm = () => {
  validateField('name');
  validateField('email');
  validateField('subject');
  validateField('message');
  
  return !Object.values(errors).some(error => error !== '');
};

const handleSubmit = async () => {
  if (!validateForm()) return;
  
  isSubmitting.value = true;
  
  try {
    // 这里替换为实际的API调用
    // await axios.post('/api/contact', formData);
    
    // 模拟API调用延迟
    await new Promise(resolve => setTimeout(resolve, 1500));
    
    submitSuccess.value = true;
    formData.name = '';
    formData.email = '';
    formData.subject = '';
    formData.message = '';
  } catch (error) {
    console.error('提交失败:', error);
    alert('提交失败，请稍后再试');
  } finally {
    isSubmitting.value = false;
  }
};
</script>

<style scoped>
.contact-us {
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem;
  font-family: Arial, sans-serif;
}

h1 {
  color: #333;
  text-align: center;
  margin-bottom: 0.5rem;
}

.subtitle {
  text-align: center;
  color: #666;
  margin-bottom: 2rem;
}

.contact-form {
  background: #f9f9f9;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  margin-bottom: 2rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: bold;
  color: #444;
}

input, textarea {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

input:focus, textarea:focus {
  outline: none;
  border-color: #4a90e2;
  box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
}

.error {
  border-color: #e74c3c !important;
}

.error-message {
  color: #e74c3c;
  font-size: 0.875rem;
  margin-top: 0.25rem;
  display: block;
}

.submit-btn {
  background: #4a90e2;
  color: white;
  border: none;
  padding: 0.75rem 1.5rem;
  font-size: 1rem;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.3s;
  width: 100%;
}

.submit-btn:hover {
  background: #357abd;
}

.submit-btn:disabled {
  background: #cccccc;
  cursor: not-allowed;
}

.success-message {
  background: #d4edda;
  color: #155724;
  padding: 1rem;
  border-radius: 4px;
  text-align: center;
  margin-bottom: 2rem;
}

.contact-info {
  background: #f0f8ff;
  padding: 1.5rem;
  border-radius: 8px;
}

.contact-info h3 {
  margin-top: 0;
  color: #333;
}

.contact-info p {
  margin: 0.5rem 0;
  color: #555;
}
</style>